package com.zyf.school.manager.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zyf.school.manager.domain.Course;
import com.zyf.school.manager.domain.Dict;
import com.zyf.school.manager.domain.DictTree;
import com.zyf.school.manager.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/course/")
public class CourseController {

    @Autowired
    CourseService courseService;

    List<Dict> dictList;
    List<Course> courseList;
    List<DictTree> dictTreeList;

    //分页
    Page<Course> page;
    public CourseController(){
        page = new Page<>(1,5);
        page.setSize(5);
    }
    //插入数据
    @RequestMapping("add")
    public String addCourse(HttpServletRequest request,Course course){
        courseService.save(course);
        QueryWrapper<Course> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id");
        page.setCurrent(1);
        page.setPages(5);
        page.setTotal(courseService.count());
        page = courseService.page(page,wrapper);
        request.setAttribute("coursePage",page);
        return "course/courseManager";


    }

    //分页查询：开始页数
    @RequestMapping("courseManager")
    public String courseManager(HttpServletRequest request,@RequestParam("pageNum") int pageNum){
        QueryWrapper<Course> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id");
        page.setSize(4);
        page.setCurrent(pageNum);
        page.setTotal(courseService.count());
        courseService.page(page,wrapper);
        request.setAttribute("coursePage",page);
        return "course/courseManager";
    }


    @RequestMapping("deleteById")
    public String deleteById(HttpServletRequest request , @RequestParam("id")int id){
        courseService.removeById(id);
        return this.courseManager(request,1);
    }

    //模糊查询
    @RequestMapping("queryByName")
    public String queryByName(HttpServletRequest request,@RequestParam("name") String name){
        QueryWrapper<Course> wrapper = new QueryWrapper<>();
        wrapper.like(true,"name",name);
        page.setSize(4);
        page.setCurrent(1);
        wrapper.orderByDesc("id");
        courseService.page(page,wrapper);
        request.setAttribute("coursePage",page);
        return "course/courseManager";
    }

}
